#include "file.h"
#include <QFile>
#include <QDataStream>
#include <QTextStream>
File::File(QObject *parent) : QObject(parent)
{

}



QString File::read(const QString &fileName)
{
    QFile file(fileName);

    file.open(QIODevice::ReadOnly);

    QDataStream in(&file);

    QString data;

    in >> data;

    return data;
}

QString File::b64Hex(const QString &fileName)
{
    QFile file(fileName);

    file.open(QIODevice::ReadOnly);

    QDataStream in(&file);

    QByteArray data;

    in >> data;

    return data.toBase64().toHex();
}

void File::write(const QString &fileName, const QString &data)
{
    QFile file(fileName);

    file.open(QIODevice::WriteOnly | QIODevice::Truncate);

    QDataStream out(&file);

    out << data;
}


QString File::readText(const QString &fileName, const QString &codecName)
{
    QFile file(fileName);

    if (!file.open(QIODevice::ReadOnly))
        return QString();

    QTextStream stream(&file);

    stream.setCodec(qPrintable(codecName));

    QString data = stream.readAll();

    return data;
}

void File::writeText(const QString &fileName, const QString &data)
{
    QFile file(fileName);

    file.open(QIODevice::WriteOnly | QIODevice::Truncate);

    QTextStream stream(&file);

    stream.setCodec("UTF-8");

    stream << data;
}
